/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						 spell_effect_window.h

	$Header: /heroes4/spell_effect_window.h $

	$NoKeywords: $

 ************************************************************************/

#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "spell_effect_window.h"

#include "combat_creature.h"
#include "enum_operations.h"
#include "scaled_animation_cache.h"
#include "spell.h"
#include "spell_properties.h"

// -----------------------------------------------------------------------
// object for holding spell effect caches
// -----------------------------------------------------------------------
namespace
{
	class t_spell_effect_cache
	{
	public:
		t_spell_effect_cache( double scale );

		t_animation_ptr get_effect( t_spell spell ) const;
		double			get_scale() const;
	protected:
		t_animation_cache	m_animations[k_spell_effect_count];
		double				m_scale;
	};
};

inline double t_spell_effect_cache::get_scale() const
{
	return m_scale;
}


// -----------------------------------------------------------------------
// object for holding spell effect caches
// -----------------------------------------------------------------------
t_spell_effect_cache::t_spell_effect_cache( double scale )
{
	m_scale = scale;

	t_spell     index;
	std::string name;

	for (index = t_spell(0); index < k_spell_effect_count; enum_incr( index ))
	{
		name = "spell.";
		name += get_spell_keyword( index );
		m_animations[index] = t_scaled_animation_cache( name, scale );
	}
}

// -----------------------------------------------------------------------
// object for holding spell effect caches
// -----------------------------------------------------------------------
t_animation_ptr t_spell_effect_cache::get_effect( t_spell spell ) const
{
	t_animation_ptr result = m_animations[spell].get();

	if (result == 0)
		result = m_animations[k_spell_misfortune].get();
	return result;
}



t_spell_effect_window::t_spell_effect_window( t_combat_creature*     creature,
											  t_animation_ptr const& bitmaps, 
											  t_screen_point point,  int speed,
		                                      t_window* parent )
					 : t_counted_animation( creature->get_battlefield() ),
					   t_animated_window( bitmaps, point, speed, parent )
{
	m_creature = creature;
}

void t_spell_effect_window::on_animation_end()
{
	m_creature->set_spell_animation( 0 );
	m_creature = 0;
	close();
	animation_ended();
}


// -----------------------------------------------------------------------
// get spell animation
// -----------------------------------------------------------------------
t_animation_ptr get_spell_animation( t_spell spell, double scale )
{
	static std::vector<t_spell_effect_cache>	caches;
	int											i;

	for (i = 0; i < caches.size(); ++i)
		if (caches[i].get_scale() == scale)
			break;
	if (i == caches.size())
		caches.push_back( t_spell_effect_cache( scale ) );
	return caches[i].get_effect( spell );
}
